package com.ffcs.cn.action.common;

import com.ffcs.cn.DevelopProperties;
import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.ui.controls.sortTable.SmSortTable;
import com.supermap.desktop.controls.utilities.ComponentFactory;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;
import com.supermap.desktop.core.utilties.TableUtilities;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * 打开地图、场景、布局的通用对话框面板
 *
  */
public abstract class AbstractPanelForm extends SmDialog {
	private JToolBar toolBar;

	private JButton buttonSelectAll;
	private JButton buttonSelectInvert;
	private JButton buttonDelete;
	private JButton buttonAdd;

	private JButton buttonOk;
	private JButton buttonCancel;
	/**
	 * 数据集、地图、布局选择表格
	 **/
	private SmSortTable itemTable;
	protected FormModel tableModel;


	public AbstractPanelForm() {
		setSize(new Dimension(400, 400));
		initComponents();
		initLayout();
		registerEvents();
	}

	private void initComponents() {
		buttonSelectAll = ComponentFactory.createToolBarButtonSelectAll();
		buttonSelectInvert = ComponentFactory.createToolBarButtonSelectInverse();
		buttonAdd = ComponentFactory.createToolBarButtonAddData();
		buttonDelete = ComponentFactory.createToolBarButtonDelete();

		toolBar = new JToolBar();
		toolBar.add(buttonAdd);
		toolBar.add(buttonDelete);
		toolBar.addSeparator();
		toolBar.add(buttonSelectAll);
		toolBar.add(buttonSelectInvert);
		toolBar.setFloatable(false);

		itemTable = new SmSortTable();
		tableModel = getTableModel();
		itemTable.setModel(tableModel);
		itemTable.setAutoCreateRowSorter(true);
		itemTable.setShowHorizontalLines(false);
		itemTable.setShowVerticalLines(false);

		buttonOk = new JButton(DevelopProperties.getString("String_Button_OK"));
		buttonOk.setEnabled(false);
		buttonCancel = new JButton(DevelopProperties.getString("String_Button_Cancel"));

	}

	private void initLayout() {
		this.setLayout(new GridBagLayout());
		this.add(toolBar, new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST)
				.setFill(GridBagConstraints.HORIZONTAL)
				.setWeight(1, 0));
		JScrollPane jScrollPane = new JScrollPane(itemTable);
		this.add(jScrollPane, new GridBagConstraintsHelper(0, 1, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.CENTER)
				.setFill(GridBagConstraints.BOTH)
				.setWeight(1, 1));
		this.add(ComponentFactory.createButtonPanel(buttonOk, buttonCancel), new GridBagConstraintsHelper(0, 2, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setFill(GridBagConstraints.NONE)
				.setAnchor(GridBagConstraints.EAST));
	}

	private void registerEvents() {
		buttonAdd.addActionListener(getAddActionListener());
		buttonDelete.addActionListener(getDeleteActionListener());
		buttonSelectAll.addActionListener(getSelectAllActionListener());
		buttonSelectInvert.addActionListener(getSelectInvertActionListener());
		buttonOk.addActionListener(getOkActionListener());
		buttonCancel.addActionListener(e -> this.dispose());
		// 未选择任何图层，禁用确定按钮
		tableModel.addTableModelListener(e -> buttonOk.setEnabled(tableModel.getRowCount() != 0));
	}

	public SmSortTable getItemTable() {
		return itemTable;
	}

	/**
	 * 为当前界面的表格设置表格数据模型
	 *
	 * @return 表格数据模型
	 */
	public abstract FormModel getTableModel();

	/**
	 * 设置当前表格添加按钮注册的监听器
	 *
	 * @return 添加按钮触发事件的监听器
	 */
	public abstract ActionListener getAddActionListener();


	/**
	 * 设置当前表格添加按钮注册的监听器
	 *
	 * @return 添加按钮触发事件的监听器
	 */
	public ActionListener getDeleteActionListener() {
		return e -> {
			int[] selectedRows = getItemTable().getSelectedRows();
			tableModel.remove(selectedRows);
			TableUtilities.setSelectedRowByDeleted(getItemTable(), selectedRows);
		};
	}

	/**
	 * 设置当前表格全选按钮注册的监听器
	 *
	 * @return 全选按钮注册的监听器
	 */
	public ActionListener getSelectAllActionListener() {
		return e -> TableUtilities.selectionAll(getItemTable());
	}

	/**
	 * 设置当前表格反选按钮注册的监听器
	 *
	 * @return 反选按钮触发事件的监听器
	 */
	public ActionListener getSelectInvertActionListener() {
		return e -> TableUtilities.invertSelection(getItemTable());
	}

	/**
	 * 设置当前表格确定按钮注册的监听器
	 *
	 * @return 确定按钮触发事件的监听器
	 */
	public abstract ActionListener getOkActionListener();

}
